Toyota Motor Corporation is a Japanese multinational automotive manufacturer headquartered in Toyota City, Aichi, Japan. It was founded by Kiichiro Toyoda and incorporated on August 28, 1937.
Nissan Motor Co. Ltd is a Japanese multinational automobile manufacturer headquartered in Yokohama, Kanagawa, Japan
Land Rover is a British brand of predominantly four-wheel drive, off-road capable vehicles, owned by multinational car manufacturer Jaguar Land Rover, since 2008 a subsidiary of India's Tata Motors. JLR currently builds Land Rovers in Brazil, China, India, Slovakia, and the United Kingdom
Mercedes-Benz, commonly referred to as Mercedes and sometimes as Benz, is a German luxury and commercial vehicle automotive brand established in 1926. Mercedes-Benz AG is headquartered in Stuttgart, Baden-Württemberg, Germany
Bayerische Motoren Werke AG, commonly abbreviated to BMW is a German multinational manufacturer of luxury vehicles and motorcycles headquartered in Munich, Bavaria, Germany
# lets import libraries
from googleapiclient.discovery import build
import pandas as pd
import seaborn as sns
Function to get Toyota channel statistics
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Toyota_channel_id = ['UC1pOTJteEef10zJM0cHs4iQ' # Toyota
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Toyota_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Toyota_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Toyota_channel_id)
[{'Channel_name': 'Toyota USA', 'Subscribers': '741000', 'Views': '194176666', 'Total_videos': '2837'}]
channel_statistics = get_channel_stats(youtube, Toyota_channel_id)
Toyota_channel_data =pd.DataFrame(channel_statistics)
Toyota_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Toyota USA | 741000 | 194176666 | 2837 |
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Nissan_channel_id = ['UCIpK0Bh0wFnC-QqgJs6hx5w' # Nissan
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Nissan_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Nissan_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Nissan_channel_id)
[{'Channel_name': 'Nissan', 'Subscribers': '224000', 'Views': '109822455', 'Total_videos': '873'}]
channel_statistics = get_channel_stats(youtube, Nissan_channel_id)
Nissan_channel_data =pd.DataFrame(channel_statistics)
Nissan_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Nissan | 224000 | 109822455 | 873 |
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Land_Rover_channel_id = ['UCZANLEnWKjMbuIkCtDUOqlA' # Land Rover
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Land_Rover_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Land_Rover_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Land_Rover_channel_id)
[{'Channel_name': 'Land Rover', 'Subscribers': '877000', 'Views': '189172542', 'Total_videos': '285'}]
channel_statistics = get_channel_stats(youtube, Land_Rover_channel_id)
Land_Rover_channel_data =pd.DataFrame(channel_statistics)
Land_Rover_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Land Rover | 877000 | 189172542 | 285 |
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Mercedes_Benz_channel_id = ['UClj0L8WZrVydk5xKOscI6-A' # Mercedes-Benz
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Mercedes_Benz_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Mercedes_Benz_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Mercedes_Benz_channel_id)
[{'Channel_name': 'Mercedes-Benz', 'Subscribers': '1860000', 'Views': '313315470', 'Total_videos': '2188'}]
channel_statistics = get_channel_stats(youtube, Mercedes_Benz_channel_id)
Mercedes_Benz_channel_data =pd.DataFrame(channel_statistics)
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Mercedes-Benz | 1860000 | 313315470 | 2188 |
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
BMW_channel_id = ['UCYwrS5QvBY_JbSdbINLey6Q' # BMW
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, BMW_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(BMW_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,BMW_channel_id)
[{'Channel_name': 'BMW', 'Subscribers': '1460000', 'Views': '241472898', 'Total_videos': '829'}]
channel_statistics = get_channel_stats(youtube, BMW_channel_id)
BMW_channel_data =pd.DataFrame(channel_statistics)
BMW_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | BMW | 1460000 | 241472898 | 829 |
# Lets combine the data frames
# Combine DataFrames using concat
combined_car_brands_channels_df = pd.concat([Toyota_channel_data ,
Nissan_channel_data ,
BMW_channel_data ,
Land_Rover_channel_data ,
Mercedes_Benz_channel_data
], ignore_index=True)
# Display the result
print(combined_car_brands_channels_df)
Channel_name Subscribers Views Total_videos 0 Toyota USA 741000 194176666 2837 1 Nissan 224000 109822455 873 2 BMW 1460000 241472898 829 3 Land Rover 877000 189172542 285 4 Mercedes-Benz 1860000 313315470 2188
# lets have a look at the datatypes
combined_car_brands_channels_df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 5 entries, 0 to 4 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Channel_name 5 non-null object 1 Subscribers 5 non-null object 2 Views 5 non-null object 3 Total_videos 5 non-null object dtypes: object(4) memory usage: 292.0+ bytes
# lets change the datatypes to perform visualisations
combined_car_brands_channels_df['Subscribers'] = pd.to_numeric(combined_car_brands_channels_df['Subscribers'])
combined_car_brands_channels_df['Views'] = pd.to_numeric(combined_car_brands_channels_df['Views'])
combined_car_brands_channels_df['Total_videos'] = pd.to_numeric(combined_car_brands_channels_df['Total_videos'])
# letsconirm if the datatypes has changed
combined_car_brands_channels_df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 5 entries, 0 to 4 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Channel_name 5 non-null object 1 Subscribers 5 non-null int64 2 Views 5 non-null int64 3 Total_videos 5 non-null int64 dtypes: int64(3), object(1) memory usage: 292.0+ bytes
combined_car_brands_channels_df
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Toyota USA | 741000 | 194176666 | 2837 |
1 | Nissan | 224000 | 109822455 | 873 |
2 | BMW | 1460000 | 241472898 | 829 |
3 | Land Rover | 877000 | 189172542 | 285 |
4 | Mercedes-Benz | 1860000 | 313315470 | 2188 |
import pandas as pd
import matplotlib.pyplot as plt
# Sort the DataFrame by 'Subscribers' in descending order
combined_car_brands_channels_df_subscribers_sorted = combined_car_brands_channels_df.sort_values(by='Subscribers', ascending=False)
# Shorten channel names
combined_car_brands_channels_df_subscribers_sorted['Channel_name'] = combined_car_brands_channels_df_subscribers_sorted['Channel_name'].apply(lambda x: x[:8])
# Plotting Subscribers in descending order
fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(10, 15))
axes[0].bar(combined_car_brands_channels_df_subscribers_sorted['Channel_name'], combined_car_brands_channels_df_subscribers_sorted['Subscribers'], color='blue')
axes[0].set_title('Subscribers')
# Sort the DataFrame by 'Views' in descending order
combined_car_brands_channels_df_views_sorted = combined_car_brands_channels_df.sort_values(by='Views', ascending=False)
# Shorten channel names
combined_car_brands_channels_df_views_sorted['Channel_name'] = combined_car_brands_channels_df_views_sorted['Channel_name'].apply(lambda x: x[:8])
# Plotting Views in descending order
axes[1].bar(combined_car_brands_channels_df_views_sorted['Channel_name'], combined_car_brands_channels_df_views_sorted['Views'], color='green')
axes[1].set_title('Views')
# Sort the DataFrame by 'Total_videos' in descending order
combined_car_brands_channels_df_total_videos_sorted =combined_car_brands_channels_df.sort_values(by='Total_videos', ascending=False)
# Shorten channel names
combined_car_brands_channels_df_total_videos_sorted['Channel_name'] = combined_car_brands_channels_df_total_videos_sorted['Channel_name'].apply(lambda x: x[:8])
# Plotting Total Videos in descending order
axes[2].bar(combined_car_brands_channels_df_total_videos_sorted['Channel_name'], combined_car_brands_channels_df_total_videos_sorted['Total_videos'], color='orange')
axes[2].set_title('Total Videos')
# Adjust layout for better visibility
plt.tight_layout()
# Show the plot
plt.show()